package hibernateDAOS;

import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import beans.Carta;
import beans.Localidad;

public class LocalidadDAO {
	
	private static LocalidadDAO instancia = null;
	private static SessionFactory sf = null;

	public static LocalidadDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new LocalidadDAO();
		} 
		return instancia;
	}
	
	public void guardarLocalidad(Localidad l){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(l);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarLocalidad(Localidad l){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(l);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarLocalidad(Localidad l){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(l);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<Carta> obtenerLocalidades(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Localidad");
		List<Carta> cartas = q.list();
		session.close();
		return cartas;
	}
}


